package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ElectronicBadRecordInfoBo;
import com.hospital.workstation.domain.vo.ElectronicBadRecordInfoVo;
import com.hospital.workstation.domain.ElectronicBadRecordInfo;
import com.hospital.workstation.mapper.ElectronicBadRecordInfoMapper;
import com.hospital.workstation.service.IElectronicBadRecordInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 不良反应/事件记录表Service业务层处理
 *
 * @author hospital
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class ElectronicBadRecordInfoServiceImpl implements IElectronicBadRecordInfoService {

    private final ElectronicBadRecordInfoMapper baseMapper;

    /**
     * 查询不良反应/事件记录表
     */
    @Override
    public ElectronicBadRecordInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询不良反应/事件记录表列表
     */
    @Override
    public TableDataInfo<ElectronicBadRecordInfoVo> queryPageList(ElectronicBadRecordInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ElectronicBadRecordInfo> lqw = buildQueryWrapper(bo);
        Page<ElectronicBadRecordInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不良反应/事件记录表列表
     */
    @Override
    public List<ElectronicBadRecordInfoVo> queryList(ElectronicBadRecordInfoBo bo) {
        LambdaQueryWrapper<ElectronicBadRecordInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicBadRecordInfo> buildQueryWrapper(ElectronicBadRecordInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicBadRecordInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonitorId() != null, ElectronicBadRecordInfo::getMonitorId, bo.getMonitorId());
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), ElectronicBadRecordInfo::getGoodsName, bo.getGoodsName());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorizeNo()), ElectronicBadRecordInfo::getAuthorizeNo, bo.getAuthorizeNo());
        lqw.like(StringUtils.isNotBlank(bo.getGeneralName()), ElectronicBadRecordInfo::getGeneralName, bo.getGeneralName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductFactory()), ElectronicBadRecordInfo::getProductFactory, bo.getProductFactory());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), ElectronicBadRecordInfo::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getUsageAmount()), ElectronicBadRecordInfo::getUsageAmount, bo.getUsageAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getBeginEndDate()), ElectronicBadRecordInfo::getBeginEndDate, bo.getBeginEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getUsePillsReason()), ElectronicBadRecordInfo::getUsePillsReason, bo.getUsePillsReason());
        return lqw;
    }

    /**
     * 新增不良反应/事件记录表
     */
    @Override
    public Boolean insertByBo(ElectronicBadRecordInfoBo bo) {
        ElectronicBadRecordInfo add = BeanUtil.toBean(bo, ElectronicBadRecordInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改不良反应/事件记录表
     */
    @Override
    public Boolean updateByBo(ElectronicBadRecordInfoBo bo) {
        ElectronicBadRecordInfo update = BeanUtil.toBean(bo, ElectronicBadRecordInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicBadRecordInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除不良反应/事件记录表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
